package drds.configuration.mvc.core;


import drds.configuration.mvc.aop.InterceptorList;
import drds.configuration.mvc.config.UserConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 框架初始化顺序:initActionMapping()initActiveRecord()
 */
public class WebContent {

    public static final String SERVLET_CONTEXT = "servlet_context";
    /**
     * 分割线
     */

    public static final InterceptorList interceptorList = new InterceptorList();
    public static Logger logger = LoggerFactory.getLogger(WebContent.class);
    public static Map<String, Object> staticMap = new ConcurrentHashMap<String, Object>();
    public static UserConfig userConfig;

    /**
     * 集成Spring
     */
    public static WebApplicationContext getWebApplicationContext() {
        ServletContext servletContext = (ServletContext) WebContent.staticMap.get(WebContent.SERVLET_CONTEXT);
        if (servletContext == null) {
            throw new NullPointerException("servletContext");
        }
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        return webApplicationContext;
    }

    /**
     * 设置用户项目开发配置到框架配置中 : interceptors plugins
     */
    public static boolean initWebContent(UserConfig userConfig) {
        WebContent.userConfig = userConfig;
        // jsp存放的文件
        logger.info("====================配置jsp存放的文件====================");
        userConfig.configRoute();
        // 基础性拦截器
        logger.info("====================配置基础性拦截器====================");
        userConfig.configInterceptor(WebContent.interceptorList);
        // ActionMapping构建
        logger.info("====================构建ActionMapping====================");
        ActionMapping.buildActionMapping(WebContent.interceptorList.getInterceptorList());
        logger.info("====================框架信息构建完毕====================");
        return true;
    }


}
